//
//  RequestExtensions.m
//  WebClipper
//
//  Created by Daniel Braun on 21/04/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "RequestExtensions.h"
#import "StringExtension.h"


@implementation NSString (WCRequestExtensions)
- (NSMutableDictionary *) htmlRequestVariables
{
	NSMutableDictionary *d=[NSMutableDictionary dictionaryWithCapacity:10];
	NSArray *c=[self componentsSeparatedByString:@"&"];
	unsigned int i, count = [c count];
	for (i = 0; i < count; i++) {
		NSString * sv = [c objectAtIndex:i];
		NSString *n, *v;
		n=[sv stringBetween:nil and:@"=" inclusive:NO];
		v=[sv stringBetween:@"=" and:nil inclusive:NO];
		[d setObject:v forKey:n];
	}
	return d;
}

@end


@implementation NSURLRequest (WCRequestExtensions)


- (NSDictionary *) htmlRequestVariables
{
	NSURL *u=[self URL];
	NSString *su=[[u absoluteString] stringBetween:@"?" and:nil inclusive:NO];
	NSString *m=[self HTTPMethod];
	NSMutableDictionary *d=[su htmlRequestVariables];
	if ([m isEqualToString:@"POST"]) {
		NSString *s=[[[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding]autorelease];
		NSMutableDictionary *d2=[s htmlRequestVariables];
		if (!d) {
			d=d2;
		} else if (d2)  {
			[d addEntriesFromDictionary:d2];
		}
	}
	return d;
}

@end
